home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Technology Seed / Jan. '98 ATS.toast / NavServices1.0b3 / Navigation Services SDK / Examples / SimpleText / SimpleText ƒ / NavigationServicesSupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-12  |  7.6 KB  |  328 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        NavigationServicesSupport.c
  3.  
  4.     Contains:    Code to support Navigation Services in SimpleText
  5.  
  6.     Version:    1.0
  7.  
  8.     Copyright:    © 1997-1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Yan Arrouye
  13.  
  14.         Other Contact:        Yan Arrouye
  15.  
  16.         Technology:            OS Technologies
  17.  
  18.     Writers:
  19.  
  20.         (KLM)    Keith Mortensen
  21.         (Yan)    Yan Arrouye
  22.  
  23.     Change History (most recent first):
  24.  
  25.         <24>      1/5/98    KLM        fixed messed up header
  26.         <23>      1/5/98    KLM        added #include <Processes.h>
  27.         <22>    12/23/97    KLM        updates: now using kNav/Nav
  28.         <21>     11/5/97    KLM        App name in window title
  29.         <20>     11/4/97    KLM        App name in dialog titles
  30.         <19>    10/20/97    KLM        more a6 interface changes
  31.         <18>    10/13/97    KLM        a6 interface changes
  32.         <17>    /19/1997    Yan        Mixed mode support
  33.         <15>     5/23/97    sjf 
  34.         <14>     5/15/97    Yan        Updated for new API
  35.         <13>     5/08/97    KLM        Fixed NewOpenHandle bug, it was never creating a open rsrc.
  36.         <12>     5/08/97    Yan        Use isStationery flag
  37.         <11>     4/21/97    Yan        
  38.         <10>     4/21/97    Yan        NavGetDefaultDialogsOptions really
  39.          <9>     4/21/97    Yan        Use new NavSetDefaultDialogsOptions
  40.          <8>     4/21/97    Yan        Implemented isStationery in save
  41.          <7>     4/21/97    Yan        Updated for latest include
  42.          <6>     4/18/97    Yan        Rev'ed for new API
  43.          <5>     4/14/97    Yan        Added a NULL version of this file for classic 68K runtime (not
  44.                                     supported)
  45. */
  46.  
  47. #include "NavigationServicesSupport.h"
  48.  
  49. #include <CodeFragments.h>
  50. #include <Finder.h>
  51. #include <Dialogs.h>
  52. #include <LowMem.h>
  53. #include <string.h>
  54. #include <Processes.h>
  55.  
  56.  
  57. static Handle NewOpenHandle(OSType applicationSignature, short numTypes, OSType typeList[])
  58. {
  59.     Handle hdl = NULL;
  60.     
  61.     if ( numTypes > 0 )
  62.     {
  63.     
  64.         hdl = NewHandle(sizeof(NavTypeList) + numTypes * sizeof(OSType));
  65.     
  66.         if ( hdl != NULL )
  67.         {
  68.             NavTypeListHandle open        = (NavTypeListHandle)hdl;
  69.             
  70.             (*open)->componentSignature = applicationSignature;
  71.             (*open)->osTypeCount        = numTypes;
  72.             BlockMoveData(typeList, (*open)->osType, numTypes * sizeof(OSType));
  73.         }
  74.     }
  75.     
  76.     return hdl;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. static OSStatus SendOpenAE(AEDescList list)
  83. {
  84.     OSStatus        err;
  85.     AEAddressDesc    theAddress;
  86.     AppleEvent        dummyReply;
  87.     AppleEvent        theEvent;
  88.     
  89.     theAddress.descriptorType    = typeNull;
  90.     theAddress.dataHandle        = NULL;
  91.  
  92.     do {
  93.         ProcessSerialNumber psn;
  94.     
  95.         err = GetCurrentProcess(&psn);
  96.         if ( err != noErr) break;
  97.         
  98.         err =AECreateDesc(typeProcessSerialNumber, &psn, sizeof(ProcessSerialNumber), &theAddress);
  99.         if ( err != noErr) break;
  100.             
  101.         dummyReply.descriptorType    = typeNull;
  102.         dummyReply.dataHandle        = NULL;
  103.  
  104.         err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  105.         if ( err != noErr) break;
  106.         
  107.         err = AEPutParamDesc(&theEvent, keyDirectObject, &list);
  108.         if ( err != noErr) break;
  109.         
  110.         err = AESend(&theEvent, &dummyReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  111.         if ( err != noErr) break;
  112.         
  113.             
  114.     } while (false);
  115.     
  116.     return err;
  117. }
  118.  
  119.  
  120.  
  121. OSStatus OpenFileDialog(OSType applicationSignature, short numTypes, OSType typeList[], NavEventProcPtr eventProc, FSSpec* fileSpec, OSType* fileType)
  122. {
  123.     NavReplyRecord        theReply;
  124.     NavDialogOptions    dialogOptions;
  125.     OSErr                theErr        = noErr;
  126.     NavTypeListHandle    openList    = NULL;
  127.     NavEventUPP            eventUPP    = NewNavEventProc(eventProc);
  128.  
  129.     NavGetDefaultDialogOptions(&dialogOptions);
  130.  
  131.     BlockMoveData(LMGetCurApName(), dialogOptions.clientName, LMGetCurApName()[0] + 1);
  132.     
  133.     openList = (NavTypeListHandle)NewOpenHandle(applicationSignature, numTypes, typeList);
  134.     if ( openList )
  135.     {
  136.         HLock((Handle)openList);
  137.     }
  138.     
  139.     theErr = NavGetFile(NULL, &theReply, &dialogOptions, eventUPP, NULL, NULL, openList, NULL);
  140.     DisposeRoutineDescriptor(eventUPP);
  141.     
  142.     if (theReply.validRecord)
  143.     {
  144.         // grab the target FSSpec from the AEDesc for opening:    
  145.         AEDesc     resultDesc;
  146.         FInfo    fileInfo;
  147.  
  148.         // Is this a single file open
  149.         if ( fileSpec != NULL )
  150.         {
  151.             AEKeyword keyword;
  152.             
  153.             theErr = AEGetNthDesc(&theReply.selection, 1, typeFSS, &keyword, &resultDesc);
  154.             if (theErr == noErr)
  155.                 BlockMove(*resultDesc.dataHandle,fileSpec,sizeof(FSSpec));
  156.                 
  157.             // decide if the doc we fileSpec opening is a PICT or TEXT
  158.             theErr = FSpGetFInfo(fileSpec, &fileInfo);
  159.             if (theErr == noErr)
  160.             {
  161.                 if ( fileType != NULL )
  162.                     *fileType = fileInfo.fdType;
  163.             }
  164.         }
  165.         else
  166.         {
  167.             // Multiple files open: use ApleEvents
  168.             theErr = SendOpenAE(theReply.selection);
  169.         }
  170.  
  171.         NavDisposeReply(&theReply);
  172.     }
  173.     else
  174.     {
  175.         theErr = userCanceledErr;
  176.     }
  177.  
  178.     if (openList != NULL)
  179.     {
  180.         HUnlock((Handle)openList);
  181.         DisposeHandle((Handle)openList);
  182.     }
  183.     
  184.     return theErr;
  185. }
  186.  
  187.  
  188. short ConfirmSaveDialog(StringPtr documentName, Boolean quitting, NavEventProcPtr eventProc)
  189. {
  190.     OSStatus                theStatusErr     = noErr;
  191.     OSErr                     theErr             = noErr;
  192.     NavAskSaveChangesResult    reply             = 0;
  193.     NavAskSaveChangesAction    action             = 0;
  194.     NavEventUPP                eventUPP        = NewNavEventProc(eventProc);
  195.     NavDialogOptions        dialogOptions;
  196.     short                    result;
  197.     
  198.     if (quitting)
  199.         action = kNavSaveChangesQuittingApplication;
  200.     else
  201.         action = kNavSaveChangesClosingDocument;
  202.         
  203.     BlockMoveData(LMGetCurApName(),dialogOptions.clientName,LMGetCurApName()[0]+1);
  204.     BlockMoveData(documentName,dialogOptions.savedFileName,documentName[0]+1);
  205.     
  206.     theErr = NavAskSaveChanges(    &dialogOptions,
  207.                                 action,
  208.                                 &reply,
  209.                                 eventUPP,
  210.                                 NULL);
  211.     DisposeRoutineDescriptor(eventUPP);
  212.     
  213.     // Map reply code to ok, cancel, dontSave
  214.     switch (reply)
  215.     {
  216.         case kNavAskSaveChangesSave:
  217.             result = ok;
  218.             break;
  219.             
  220.         case kNavAskSaveChangesCancel:
  221.             result = cancel;
  222.             break;
  223.             
  224.         case kNavAskSaveChangesDontSave:
  225.             result = dontSaveChanges;
  226.             break;
  227.     }
  228.     
  229.     return result;
  230. }
  231.  
  232.  
  233.  
  234. OSStatus SaveFileDialog(StringPtr fileName, OSType filetype, OSType fileCreator, 
  235.                         NavEventProcPtr eventProc, FSSpec* fileSpec, 
  236.                         Boolean* stationery, Boolean* replacing, NavReplyRecord* reply)
  237. {
  238.     NavDialogOptions    dialogOptions;
  239.     OSErr                theErr        = noErr;
  240.     NavEventUPP            eventUPP    = NewNavEventProc(eventProc);
  241.  
  242.     NavGetDefaultDialogOptions(&dialogOptions);
  243.  
  244.     dialogOptions.dialogOptionFlags |= (stationery != NULL ? kNavAllowStationery : 0);
  245.     BlockMoveData(fileName, dialogOptions.savedFileName, fileName[0] + 1);
  246.     BlockMoveData(LMGetCurApName(), dialogOptions.clientName, LMGetCurApName()[0] + 1);
  247.  
  248.     theErr = NavPutFile(NULL, reply, &dialogOptions, eventUPP, filetype, fileCreator, NULL);
  249.     DisposeRoutineDescriptor(eventUPP);
  250.     
  251.     if (reply->validRecord)
  252.     {
  253.         // User saved
  254.         AEDesc     resultDesc;
  255.         AEKeyword keyword;
  256.             
  257.         // retrieve the returned selection:
  258.         theErr = AEGetNthDesc(&reply->selection, 1, typeFSS, &keyword, &resultDesc);
  259.         if (theErr == noErr)
  260.             BlockMove(*resultDesc.dataHandle, fileSpec, sizeof(FSSpec));
  261.  
  262.         if ( replacing != NULL )
  263.             *replacing = reply->replacing;
  264.         
  265.         if ( stationery != NULL )
  266.         {
  267.             *stationery    = reply->isStationery;
  268.         }
  269.     }
  270.     else
  271.     {
  272.         // User cancelled
  273.         if ( replacing != NULL )
  274.             *replacing = false;
  275.         
  276.         if ( stationery != NULL )
  277.             *stationery    = false;    
  278.  
  279.         theErr = userCanceledErr;
  280.     }
  281.     
  282.     return theErr;
  283. }
  284.  
  285.  
  286.  
  287. OSStatus CompleteSave(const FSSpec*, NavReplyRecord* reply)
  288. {
  289.     OSStatus theErr;
  290.     
  291.     if (reply->validRecord)
  292.     {
  293.         theErr = NavCompleteSave(reply, kNavTranslateInPlace);
  294.     }
  295.  
  296.     theErr = NavDisposeReply(reply);
  297.     
  298.     return theErr;
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307. //
  308. // Callback to handle events that occur while navigation dialogs are up but really should be handled by the application
  309. //
  310.  
  311. extern void HandleEvent(EventRecord * pEvent);
  312.  
  313.  
  314. pascal void MyEventProc(const NavEventCallbackMessage callBackSelector, 
  315.                         NavCBRecPtr callBackParms, 
  316.                         NavCallBackUserData /*callBackUD*/)
  317. // Callback to handle event passing betwwn the navigation dialogs and the applicatio
  318. {
  319.     if ( callBackSelector == kNavCBEvent )
  320.         switch (callBackParms->eventData.event->what)
  321.         {
  322.             case updateEvt:
  323.                 HandleEvent(callBackParms->eventData.event);
  324.                 break;
  325.         }
  326. }
  327.  
  328.